package com.cskaoyan.controller.backstagecontroller;


import com.cskaoyan.bean.bo.AuthPermissionsBO;
import com.cskaoyan.bean.pojo.Permission;
import com.cskaoyan.bean.pojo.Role;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.RoleListVo;
import com.cskaoyan.bean.vo.RolePermissionVO;
import com.cskaoyan.service.AdminRoleService;
import com.cskaoyan.service.AdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/role/")
public class AdminRoleManagementController {


    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    AdminService adminService;


    @RequestMapping("list")
    public BaseRespVo getAllAdminRole(Integer page, Integer limit, String sort, String order, String name) {

        RoleListVo roleListVo = adminRoleService.searchAdminRole(page, limit, sort, order, name);

        return BaseRespVo.ok(roleListVo, "成功");

    }

    @RequestMapping("create")
    public BaseRespVo createAdminRole(@RequestBody Role role) {

        Role newRole = adminRoleService.addAdminRole(role);

        if (newRole != null) return BaseRespVo.ok(newRole, "成功");

        return BaseRespVo.fail("失败");
    }


    @RequestMapping("update")
    public BaseRespVo updateAdminRole(@RequestBody Role role) {

        Role newRole = adminRoleService.updateAdminRole(role);

        if (newRole != null) return BaseRespVo.ok(newRole, "成功");

        return BaseRespVo.fail("失败");
    }

    @RequestMapping("delete")
    public BaseRespVo deleteAdminRole(@RequestBody Role role) {
        Role role1 = adminRoleService.deleteAdminRole(role);
        if (role1 != null) return BaseRespVo.ok();
        return BaseRespVo.fail("修改失败");

    }

    @GetMapping("permissions")
    @RequiresPermissions("admin:role:permissions")
    public BaseRespVo permissions(Integer roleId) {
        System.out.println("get中的roleId: " + roleId);//1
        RolePermissionVO rolePermissionVO = adminService.getPermissions(roleId);

        return BaseRespVo.ok(rolePermissionVO);
    }

    @PostMapping("permissions")
    @RequiresPermissions("admin:role:permissions:update")
    public BaseRespVo rolePermission(@RequestBody AuthPermissionsBO permissionsBO) {
        int roleId = permissionsBO.getRoleId();
        System.out.println("post中的roleId: " + roleId);
        if (roleId == 1) {
            return BaseRespVo.fail("禁止修改!");
        }
        adminService.roleAuthorization(permissionsBO);
        return BaseRespVo.ok();
    }

}
